

package cn.shengchao.examstart.user.ohs.local.appservices;

import cn.shengchao.examstart.user.acl.adapters.client.cloudauth20190307.models.ContrastFaceVerifyResponse;
import cn.shengchao.examstart.user.acl.ports.client.IRealPersonAuthenticationApi;
import cn.shengchao.examstart.user.acl.ports.repositories.IUserRepository;
import cn.shengchao.examstart.user.domain.entity.UserRealPerson;
import cn.shengchao.examstart.user.ohs.local.messages.UserRealPersonDto;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.continew.starter.core.validation.CheckUtils;

import java.io.IOException;
import java.time.LocalDateTime;

@Slf4j
@Service
public class RealPersonAppService {

    @Autowired
    private IRealPersonAuthenticationApi iRealPersonAuthenticationApi;
    @Autowired
    private IUserRepository iUserRepository;

    public void save(UserRealPersonDto userRealPerson, Long uid) throws IOException {
        log.info("实人认证调用 userId:{}", uid);
        //1.调用实人认证接口
        ContrastFaceVerifyResponse response = iRealPersonAuthenticationApi.contrastFaceVerify(String
            .valueOf(uid), userRealPerson.getRealName(), userRealPerson.getIdcard(), userRealPerson.getRealPersonVideo()
                .getInputStream());
        if (response.getStatusCode() != 200 || !response.getBody().getCode().equals("200")) {
            log.error("实人认证验证失败 userId:{},code:{},message:{}", uid, response.getBody().getCode(), response.getBody()
                .getMessage());
            switch (response.getBody().getCode()) {
                case "201":
                    CheckUtils.throwIfNull(null, "姓名和身份证不一致");
                case "202":
                    CheckUtils.throwIfNull(null, "查询不到身份信息");
                case "203":
                    CheckUtils.throwIfNull(null, "查询不到照片或照片不可用");
                case "204":
                    CheckUtils.throwIfNull(null, "人脸比对不一致");
                case "205":
                    CheckUtils.throwIfNull(null, "活体检测存在风险");
                default:
                    CheckUtils.throwIfNull(null, "实人认证失败");
            }
        }
        //2.保存实名认证结果
        log.info("实人认证验证通过 userId:{}", uid);
        UserRealPerson realPerson = new UserRealPerson();
        realPerson.setUserId(uid);
        realPerson.setRealName(userRealPerson.getRealName());
        realPerson.setIdcard(userRealPerson.getIdcard());
        realPerson.setApiResult(JSON.toJSONString(response.getBody()));
        realPerson.setTime(LocalDateTime.now());
        realPerson.setIdcardBack(userRealPerson.getIdcardBack());
        realPerson.setIdcardFront(userRealPerson.getIdcardFront());
        iUserRepository.saveRealPerson(realPerson);
    }

    public UserRealPersonDto getRealPerson(Long uid) {
        UserRealPerson userRealPerson = iUserRepository.getRealPerson(uid);
        if (userRealPerson != null) {
            return UserRealPersonDto.of(userRealPerson);
        }
        return new UserRealPersonDto();
    }
}
